import java.awt.Rectangle
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.pdmodel.PDPage
import org.apache.pdfbox.util.PDFTextStripperByArea

PDDocument pd;
BufferedWriter wr;
String[] args = ["example.pdf", "result.txt"];
 try {
		 File input = new File(args[0]);  // The PDF file from where you would like to extract
		 File output = new File(args[1]); // The text file where you are going to store the extracted data
		 pd = PDDocument.load(input);
		 System.out.println(pd.getNumberOfPages());
		 System.out.println(pd.isEncrypted());
PDFTextStripperByArea astripper = new PDFTextStripperByArea();
astripper.setSortByPosition( true );
Rectangle rect = new Rectangle( 218, 635, 332, 23);
astripper.addRegion( "class1", rect );
List allPages = pd.getDocumentCatalog().getAllPages();
PDPage p = (PDPage)allPages.get(0);
astripper.extractRegions(p);

System.out.println(p.getContents().getStream());
println p.getMediaBox().toString() + " " + p.getTrimBox().toString()
String string = astripper.getTextForRegion( "class1" );
System.out.print(string);
//         stripper.setStartPage(3); //Start extracting from page 3
//        stripper.setEndPage(5); //Extract till page 5
         wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)));
//        stripper.writeText(pd, wr);
         if (pd != null) {
             pd.close();

         }
        // I use close() to flush the stream.
        wr.close();
 } catch (Exception e){
         e.printStackTrace();
        }